from datetime import datetime

from flask_login import UserMixin
from sqlalchemy.dialects.mysql import LONGTEXT
from werkzeug.security import generate_password_hash, check_password_hash

from Config import db

class UserRo:
    def __init__(self, username:str, password:str, email:str):
        self.username = username
        self.password = password
        self.email = email


class R:
    def __init__(self, code: int, msg: str, data: dict = None):
        self.code: int = code
        self.msg: str = msg
        self.data: dict = data

    def define(self) -> dict:
        if self.data:
            return {
                "code": self.code,
                "msg": self.msg,
                "data": self.data
            }
        return {
            "code": self.code,
            "msg": self.msg
        }


class Post(db.Model):
    __tablename__ = "post"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    user_id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(100))
    content = db.Column(LONGTEXT)
    type = db.Column(db.Integer)
    status = db.Column(db.Integer)
    create_time = db.Column(db.DateTime, nullable=False, default=datetime.now())
    comment_count = db.Column(db.Integer)
    like_count = db.Column(db.Integer)

class User(db.Model, UserMixin):
    __tablename__ = "user"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    username = db.Column(db.String(50))
    password = db.Column(db.String(50))
    email = db.Column(db.String(50))
    type = db.Column(db.Integer) # 1-普通用户 99-超级用户(全部) 98-管理员(指定、点赞)
    status = db.Column(db.Integer)
    activation_code = db.Column(db.String(100))
    header_url = db.Column(db.String(200))
    password_hash = db.Column(LONGTEXT)
    create_time = db.Column(db.DateTime, nullable=False, default=datetime.now())

    def set_password(self, password: str):
        '''
        用来设置密码的方法，接受密码作为参数
        :param password:
        :return:
        '''
        self.password_hash = generate_password_hash(password)  # 将生成的密码保持到对应字段

    def validate_password(self, password) -> bool:
        '''
        用于验证密码的方法，接受密码作为参数
        :param password:
        :return:
        '''
        try:
            return check_password_hash(self.password_hash, password)  # 返回布尔值
        except Exception as e:
            print(e)
            return False

class Comment(db.Model):
    __tablename__ = "comment"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    user_id = db.Column(db.Integer, primary_key=True)
    target_user_id = db.Column(db.Integer)
    target_post_id = db.Column(db.Integer)
    content = db.Column(db.TEXT)
    create_time = db.Column(db.DateTime, nullable=False, default=datetime.now())

class Like(db.Model):
    __tablename__ = "like"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    user_id = db.Column(db.Integer, primary_key=True)
    target_user_id = db.Column(db.Integer,default=0)
    target_post_id = db.Column(db.Integer,default=0)
    target_comment_id = db.Column(db.Integer, default=0)
    create_time = db.Column(db.DateTime, nullable=False, default=datetime.now())

# 评论回复表
class Reply(db.Model):
    __tablename__ = "reply"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    user_id = db.Column(db.Integer, primary_key=True)
    comment_post_id = db.Column(db.Integer)
    target_post_id = db.Column(db.Integer)
    content = db.Column(db.TEXT)
    create_time = db.Column(db.DateTime, nullable=False, default=datetime.now())

# class House(db.Model):
#     __tablename__ = "house_info"
#     id = db.Column(db.Integer, primary_key=True)
#     title = db.Column(db.String(100))
#     rooms = db.Column(db.String(100))
#     area = db.Column(db.String(100))
#     price = db.Column(db.String(100))
#     direction = db.Column(db.String(100))
#     rent_type = db.Column(db.String(100))
#     region = db.Column(db.String(100))
#     block = db.Column(db.String(100))
#     address = db.Column(db.String(100))
#     traffic = db.Column(db.String(100))
#     publish_time = db.Column(db.Integer)
#     facilities = db.Column(db.TEXT)
#     highlights = db.Column(db.TEXT)
#     matching = db.Column(db.TEXT)
#     travel = db.Column(db.TEXT)
#     page_views = db.Column(db.TEXT)
#     landlord = db.Column(db.TEXT)
#     phone_num = db.Column(db.TEXT)
#     house_num = db.Column(db.Integer)
#
# class User(db.Model):
#     __tablename__ = "user_info"
#     id = db.Column(db.Integer, primary_key=True)
#     name = db.Column(db.String(100))
#     password = db.Column(db.String(100))
#     email = db.Column(db.String(100))
#     address = db.Column(db.String(100))
#     collect_id = db.Column(db.String(100))
#     seen_id = db.Column(db.String(100))
#
# class Recommend(db.Model):
#     __tablename__ = "house_recommend"
#     id = db.Column(db.Integer, primary_key=True)
#     user_id = db.Column(db.String(100))
#     house_id = db.Column(db.String(100))
#     title = db.Column(db.String(100))
#     address = db.Column(db.String(100))
#     block = db.Column(db.String(100))
#     score = db.Column(db.String(100))


# class User(db.Model):  # 数据库表模型
#     # 定义表名称
#     __tablename__ = "tb_user"
#     id = db.Column(db.Integer, primary_key=True)
#     name = db.Column(db.String(20), nullable=True)  # name字段可以为空
#     age = db.Column(db.Integer, nullable=False)
#     email = db.Column(db.String(100), nullable=False)
#     sex = db.Column(db.String(10), nullable=False)
#     articles = db.relationship("Article", backref="user", lazy="dynamic")
#
#
# class Article(db.Model):
#     # 定义表名称
#     __tablename__ = "tb_carticle"
#     id = db.Column(db.Integer, primary_key=True)
#     title = db.Column(db.String(100), nullable=False)
#     content = db.Column(db.String(255), nullable=False)
#     create_time = db.Column(db.DateTime, nullable=False, default=datetime.now())
#     love = db.Column(db.Integer, nullable=False)
#     click_num = db.Column(db.Integer, nullable=False)
#     # user_id 引用 tb_user 中的ID
#     user_id = db.Column(db.Integer, db.ForeignKey("tb_user.id"))
